
const Controller = require('./base_controller')
const rule = {
  accountAlias: 'string',
  accessToken: 'string',
  accountName: 'string?'
}
class CustomerController extends Controller {
  
  //查询
  async index (ctx) {
    try {
      let customerList = await ctx.service.customerService.index()
      this.success({data: customerList.rows})
    } catch (e) {
      this.error({
        msg: '查询失败!',
        data: e
      })
    }
  }
  //创建
  async create (ctx) {
    let { request: { body } } = ctx
    try {
      ctx.validate(rule)
      await ctx.service.customerService.create(body)
      this.success({
        msg: '创建成功！',
        data: body
      })
    } catch (e) {
      let msg = (e && e.invalid_param) 
        ? '参数错误!' : '创建失败!'
      this.error({
        msg: msg,
        data: e
      })
    }
  }
  //修改
  async update (ctx) {
    let { params, request: { body } } = ctx
    try {
      ctx.validate(rule)
      let customer = Object.assign(params, body)
      await ctx.service.customerService.update(customer)
      this.success({
        msg: '修改成功!',
        data: customer
      })
    } catch (e) {
      let msg = (e && e.invalid_param)
        ? '参数错误!' : '修改失败!'
      this.error({
        msg: msg,
        data: e
      })
    }
  }
}


module.exports = CustomerController
